package api_day14;

import org.junit.Test;
import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * 函数式编程练习
 */
public class ClassWork {
    @Test //函数式编程课堂练习一：Supplier
    public void work1(){
        //我的code
        /*Supplier<Integer[]> s = () -> new Integer[]{1, 2, 3, 4, 5, 6, 7};
        int max = ((GetMax) arr -> {
            int max1 = 0;
            for (int i = 0; i < arr.length; i++) {
                int temp = arr[i];
                if (temp > max1) max1 = temp;
            }
            return max1;
        }).getMax(s.get());
        System.out.println(max);*/
        //老师的答案
        int[] arr = {1,2,3,4,5,6,7,8,9};
        int max = getMax(arr, () -> {
            int temp = 0;
            for (int i : arr){
                if (i > temp) temp = i;
            }
            return temp;
        });
        System.out.println(max);
    }
    //老师的
    public static int getMax(int[] arr,Supplier<Integer> supplier){
        return supplier.get();
    }
    @Test //函数式编程课堂练习二：Consumer
    public void work2(){
        String input = "迪丽热巴,女";
        printCon(input,
                s -> System.out.println("第一次消费：姓名：" + s.split(",")[0]),
                s -> System.out.println("第二次消费：性别：" + s.split(",")[1]));
    }
    public static void printCon(String str, Consumer<String> con1,Consumer<String> con2){
        con1.andThen(con2).accept(str);
    }
    @Test //函数式编程课堂练习三：Predicate
    public void work3(){
        String[] arr = {"迪丽热巴,女","古力娜扎,女","马儿扎哈,男","赵丽颖,女"};
        ArrayList<String> choosed = choosePeople(arr,
                s -> s.split(",")[1].equals("女"),
                s -> s.split(",")[0].length() == 4);
        System.out.println(choosed);
    }
    public static ArrayList<String> choosePeople(String[] arr , Predicate<String> pre1 , Predicate<String> pre2){
        ArrayList<String> infos = new ArrayList<>();
        for (String str : arr){
            if (pre1.and(pre2).test(str)) infos.add(str);
        }
        return infos;
    }
    @Test //函数式编程课堂练习四：Function
    public void work4(){
        connect("赵丽颖,20",
                s -> Integer.parseInt(s.split(",")[1]));
    }
    public static void connect(String str,Function<String,Integer> fun){
        Integer apply = fun.apply(str);
        System.out.println(apply + 100);
    }
}
//Supplier
interface GetMax{
    int getMax(Integer[] arr);//我的
}